import {action, observable, runInAction} from "mobx";
import {BaseResInterface, BaseResInterfaceWithData, sendGetRequest, sendPostRequest} from "../../../net";
import {defaultObj, valWithDefault} from "../../../util";
import {ShippingAddressInterface} from "../../../net/instance/CommenModelInstance";
import UserStoreInstance from "./index";


interface Address {

}

export class AddressStore {
    @observable userAddress: ShippingAddressInterface = defaultObj;

    @action.bound
    async getUserShippingAddress() {
        const {data} = await sendGetRequest<BaseResInterfaceWithData<ShippingAddressInterface>>("user/getUserShippingAddress", {userId: UserStoreInstance.user.id});
        runInAction(() => {
            this.userAddress = valWithDefault(data);
        })
    }

    @action.bound
    async insertOrUpdateShippingAddress(params: ShippingAddressInterface) {
        const {data} = await sendPostRequest<BaseResInterfaceWithData<ShippingAddressInterface>>("user/insertOrUpdateAddress", {
            ...params,
            userId: UserStoreInstance.user.id,
        });
        runInAction(() => {
            this.userAddress = data;
        })
    }

    constructor() {
    }

}

const AddressStoreInstance = new AddressStore();
export default AddressStoreInstance